import 'package:flutter/material.dart';
import 'package:whoisdinting/global.dart';
import 'package:whoisdinting/view.dart';

import 'game.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Global.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
//  @override
//  Widget build(BuildContext context) {
//    return MaterialApp(
//      title: 'Who is dinting',
//      theme: ThemeData(
//        primarySwatch: Colors.blue,
//        visualDensity: VisualDensity.adaptivePlatformDensity,
//      ),
//      home: MyHomePage(title: 'Home Page'),
//    );
//  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        initialRoute: '/',
        routes: {
          '/': (context) => MyHomePage(title: '首页'),
          '/game': (context) => GamePage(),
          '/view': (context) => ViewPage(),
        });
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int count=0;
  bool selfInput=false;
  String word1;
  String word2;

  _MyHomePageState(){
    if(Global.personCount != null && Global.personCount > 0){
      count=Global.personCount;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '谁是卧底',
                  style: TextStyle(
                    color: Colors.blue,
                    fontSize: 28.0,
                  ),
                ),
                SizedBox(height: 50),
                TextField(
                    autofocus: true,
                    controller: new TextEditingController(text: count >0 ? count.toString() : ""),
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                      labelText: "人数",
                    ),
                    onChanged: (v) {
                      count=int.parse(v);
                      print("onChange: $v");
                    }
                ),
                SwitchListTile(
                  title: Text("自己输入"),
                  value: selfInput,
                  onChanged:(value){
                    setState(() {
                      selfInput=value;
                    });
                    print(selfInput);
                  },
                  // onChanged: _changed,
                ),
                if(selfInput) TextField(
                    decoration: InputDecoration(
                      labelText: "词语一",
                    ),
                    onChanged: (v) {
                      setState(() {
                        word1=v;
                      });
                      print("onChange: $v");
                    }
                ),
                if(selfInput) TextField(
                    decoration: InputDecoration(
                      labelText: "词语二",
                    ),
                    onChanged: (v) {
                      setState(() {
                        word2=v;
                      });
                      print("onChange: $v");
                    }
                ),
                FlatButton(
                  color: Colors.blue,
                  highlightColor: Colors.blue[700],
                  colorBrightness: Brightness.dark,
                  splashColor: Colors.grey,
                  child: Text("开始"),
                  shape:RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                  onPressed: () async{
                    //弹出对话框并等待其关闭
                    // List list = await showInputDialog();
                    // print("result $list");
                    // if (list == null) {
                    //   print("已取消");
                    // } else {
                    //   print("已确认");
                    //   Global.personCount=list[0];
                    //   Navigator.pushNamed(context, "/game");
                    // }
                    Global.personCount=count;
                    Global.selfInput=selfInput;
                    if(selfInput){
                      Global.add(word1, word2);
                    }
                    Navigator.pushNamed(context, "/view");
                  },
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future<List> showInputDialog() {
    int count=0;
    bool selfInput=false;
    String word1;
    String word2;

    return showDialog<List>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text("提示"),
          content: SingleChildScrollView(
            child: Column(
              children: [
                TextField(
                    autofocus: true,
                    decoration: InputDecoration(
                      labelText: "人数",
                    ),
                    onChanged: (v) {
                      count=int.parse(v);
                      print("onChange: $v");
                    }
                ),
                SwitchListTile(
                  title: Text("自己输入"),
                  value: selfInput,
                  onChanged:(value){
                    //重新构建页面
                    (context as Element).markNeedsBuild();
                    selfInput=value;
                    print(selfInput);
                  },
                  // onChanged: _changed,
                ),
                if(selfInput) TextField(
                    decoration: InputDecoration(
                      labelText: "词语一",
                    ),
                    onChanged: (v) {
                      word1=v;
                      print("onChange: $v");
                    }
                ),
                if(selfInput) TextField(
                    decoration: InputDecoration(
                      labelText: "词语二",
                    ),
                    onChanged: (v) {
                      word2=v;
                      print("onChange: $v");
                    }
                ),
              ],
            ),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: () => Navigator.of(context).pop(null), // 关闭对话框
            ),
            FlatButton(
              child: Text("确定"),
              onPressed: () {
                //关闭对话框并返回true
                Navigator.of(context).pop([count,selfInput,word1,word2]);
              },
            ),
          ],
        );
      },
    );
  }
}
